Calls a subroutine that is indicated by a label from a subroutine or a function. The statements following the label are executed until the next Return statement. Afterwards, the program continues with the statement that follows the GoSub statement.
サブルーチンや関数中から、ラベルで指定するサブルーチンを呼び出します。ラベルの指定先にあるステートメント群は、最初に出現する Return ステートメントの位置までが実行されます。これらのステートメント群の実行後、プログラムの処理行は、呼び出し元の GoSub ステートメントの次行にあたるステートメントに戻ります。
see Parameters
パラメータを参照
Sub/Function
Sub/Function
statement block
ステートメントブロック
Label
Label
statement block
ステートメントブロック
GoSub Label
GoSub Label
Exit Sub/Function
Exit Sub/Function
Label:
Label:
statement block
ステートメントブロック
Return
Return
End Sub/Function
End Sub/Function
The GoSub statement calls a local subroutine indicated by a label from within a subroutine or a function. The name of the label must end with a colon (":").
GoSub ステートメントは、ラベルで指定するローカルサブルーチンを、サブルーチンや関数内で呼び出します。ラベル位置の指定では、ラベル名に続けて末尾にコロン (:) を付ける必要があります。
![]() | If the program encounters a Return statement not preceded by GoSub, Office Basic returns an error message. Use Exit Sub or Exit Function to ensure that the program leaves a Sub or Function before reaching the next Return statement.
プログラムの実行時に、対応する GoSub の存在しない Return ステートメントに遭遇すると、Office Basic からエラーメッセージが返されます。 こうした不適切な Return ステートメントの実行を避けるには、必要な箇所に Exit Sub ないし Exit Function を記述して、該当する Sub や Function を明示的に終了させる必要があります。 |
The following example demonstrates the use of GoSub and Return. By executing a program section twice, the program calculates the square root of two numbers that are entered by the user.
下記の例は、GoSub と Return を使用したサンプルコードです。ここではユーザーに 2 つの値を入力させていますが、これらの平方根の計算は、共通の処理セクションを 2 度呼び出すことで処理しています。
Sub ExampleGoSub
Sub ExampleGoSub
dim iInputa as Single
dim iInputa as Single
dim iInputb as Single
dim iInputb as Single
dim iInputc as Single
dim iInputc as Single
iInputa = Int(InputBox$ "Enter the first number: ","NumberInput"))
iInputa = Int(InputBox$ ("Enter the first number:","NumberInput"))
iInputb = Int(InputBox$ "Enter the second number: ","NumberInput"))
iInputb = Int(InputBox$ ("Enter the second number:","NumberInput"))
iInputc=iInputa
iInputc=iInputa
GoSub SquareRoot
GoSub SquareRoot
Print "The square root of";iInputa;" is";iInputc
Print "The square root of";iInputa;" is";iInputc
iInputc=iInputb
iInputc=iInputb
GoSub SquareRoot
GoSub SquareRoot
Print "The square root of";iInputb;" is";iInputc
Print "The square root of";iInputb;" is";iInputc
Exit Sub
Exit Sub
SquareRoot:
SquareRoot:
iInputc=sqr(iInputc)
iInputc=sqr(iInputc)
Return
Return
End Sub
End Sub